﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class PurchasedetailsS : PurchasedetailsSI
    {
        private readonly WmssystemContext myDb;

        public PurchasedetailsS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }

        public async Task<int> Add(PO_Purchasedetails pO_Purchasedetails)
        {
            myDb.PO_Purchasedetails.Add(pO_Purchasedetails); // 确保不显式指定标识列的值
            return await myDb.SaveChangesAsync();
        }

        public Task<int> Del(int id)
        {
           var pp=  myDb.PO_Purchasedetails.SingleOrDefault(e => e.P_DSerialNumber == id);
            myDb.PO_Purchasedetails.Remove(pp);
            return myDb.SaveChangesAsync();
        }

      

        public async Task<List<PO_Purchasedetails>> Qu(string dh)
        {
            // return myDb.PO_Purchasedetails.OrderByDescending(p => p.P_DSerialNumber).ToListAsync();
            var result = await myDb.PO_Purchasedetails
     .Where(p => p.P_DPurchasCode == dh)  // 根据条件过滤结果
     .OrderByDescending(p => p.P_DSerialNumber)  // 根据 P_DSerialNumber 排序
     .ToListAsync();  // 转换为列表

            return result;
        }

        public async Task<int> Upd1(Vawewe vawewe)
        {
            // 使用 LINQ 查询语句检索相应的记录
            var detailsToUpdate = await myDb.PO_Purchasedetails
                .Where(d => vawewe.Ids.Contains(d.P_DSerialNumber))
                .ToListAsync();

            // 遍历查询结果，将空属性值填充
            foreach (var detail in detailsToUpdate)
            {
                // 找到与当前 detail 对应的更新值
                var updatedValue = vawewe.updatedValues.FirstOrDefault(u => u.P_DSerialNumber == detail.P_DSerialNumber);
                if (updatedValue != null)
                {
                    // 只更新指定的字段
                    if (updatedValue.P_DQuantity.HasValue)
                    {
                        detail.P_DQuantity = updatedValue.P_DQuantity.Value;
                    }
                    if (updatedValue.P_DAmount.HasValue)
                    {
                        detail.P_DAmount = updatedValue.P_DAmount.Value;
                    }
                    if (updatedValue.P_DQuantityPending.HasValue)
                    {
                        detail.P_DQuantityPending = updatedValue.P_DQuantityPending.Value;
                    }
                    if (!string.IsNullOrEmpty(updatedValue.P_DRemarks))
                    {
                        detail.P_DRemarks = updatedValue.P_DRemarks;
                    }
                    if (!string.IsNullOrEmpty(updatedValue.P_DPurchasCode))
                    {
                        detail.P_DPurchasCode = updatedValue.P_DPurchasCode;
                    }

                    // 将更新后的记录标记为已修改
                    myDb.PO_Purchasedetails.Update(detail);
                }
            }

            // 保存修改到数据库
            return await myDb.SaveChangesAsync();
        }
    }
}
